package com.cloudsigns.factorymanager2.engineering.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;

import com.cloudsigns.factorymanager2.engineering.activity.EngineeringInstallDailyFeedBackDetailActivity;
import com.cloudsigns.factorymanager2.engineering.adapter.DailyFeedBackForEngineeringInstallAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringDailyFeedBackBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;

import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;

/**
 * Created by tony on 2018/5/10.
 */

public class EngineeringInstallDailyFeedBackForSuperFragment extends Fragment implements ItemClickListener {
    private View rl_abnormal_return;
    private ListView lv_abnormal_return;
    private ScrollView scrollview;
    private LinearLayout ll_feedback;
    private List<String> dayStrList;
    private HashMap<String, List<EngineeringDailyFeedBackBean>> feedBackMap;
    private LayoutInflater layoutInflater;
    private EngineeringInstallationOrderBean engineeringInstallationOrderBean;
    private String TAG = EngineeringInstallDailyFeedBackForSuperFragment.class.getSimpleName();
    private boolean isFirst = true;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_engineeringinstall_dailyfeedback_for_super, null);
        rl_abnormal_return = view.findViewById(R.id.rl_abnormal_return);
        lv_abnormal_return = view.findViewById(R.id.lv_abnormal_return);
        ll_feedback = view.findViewById(R.id.ll_feedback);
        scrollview = view.findViewById(R.id.scrollview);
        layoutInflater = inflater;
        return view;
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && isFirst) {
            // 相当于onResume()方法
            initData();
            isFirst = false;
        } else {
            // 相当于onpause()方法
        }
    }

    public void setData(EngineeringInstallationOrderBean orderBean) {
        engineeringInstallationOrderBean = orderBean;
    }

    private void initData() {
        if (null != engineeringInstallationOrderBean) {
            feedBackMap = new HashMap<>();
            List<EngineeringDailyFeedBackBean> allEngineeringDailyFeedbackList = engineeringInstallationOrderBean.getEngineeringDailyFeedbackList();
            if (null != allEngineeringDailyFeedbackList) {

                for (EngineeringDailyFeedBackBean engineeringDailyFeedBackBean : allEngineeringDailyFeedbackList) {
                    String key = DateUtil.getYyyy_Mm_Dd(engineeringDailyFeedBackBean.getFeedbackDate());
                    if (feedBackMap.containsKey(key)) {
                        feedBackMap.get(key).add(engineeringDailyFeedBackBean);
                    } else {
                        List<EngineeringDailyFeedBackBean> engineeringDailyFeedBackBeanList = new ArrayList<>();
                        engineeringDailyFeedBackBeanList.add(engineeringDailyFeedBackBean);
                        feedBackMap.put(key, engineeringDailyFeedBackBeanList);
                    }

                }
            }
//            engineeringInstallationOrderBean = orderBean;
            long beginDate = engineeringInstallationOrderBean.getBeginDate();
//            long completeDate = engineeringInstallationOrderBean.getCompleteDate();
//            long currentDate = System.currentTimeMillis();
//            long minDate = Math.min(currentDate, completeDate);
//            int days = DateUtil.getBetweenDays(beginDate, minDate);
            beginDate = DateUtil.getMillis("2018-05-06");
            if (beginDate > 0) {
                long currentDate = System.currentTimeMillis();
                int days = DateUtil.getBetweenDays(beginDate, currentDate);


                dayStrList = new ArrayList<>();
                if (days >= 0) {

                    for (int i = days; i >= 0; i--) {
                        long nextDate = beginDate + i * 24 * 60 * 60 * 1000;
                        dayStrList.add(DateUtil.getYyyy_Mm_Dd(nextDate));
                    }

                }
            }


            updateUi();
        }
    }

    private void updateUi() {
        if (null != dayStrList && null != ll_feedback) {
            for (String date : dayStrList) {
                View view = layoutInflater.inflate(R.layout.layout_daily_feedback_for_engineeringinstall, null);
                TextView tv_date = view.findViewById(R.id.tv_date);
                TextView tv_week = view.findViewById(R.id.tv_week);
                ImageView img_add_feedback = view.findViewById(R.id.img_add_feedback);
                ListView lv_feedback = view.findViewById(R.id.lv_feedback);

                tv_date.setText(date);
                String week = DateUtil.getWeekByDateStr(date);
                tv_week.setText(week);
                img_add_feedback.setVisibility(View.GONE);

                final List<EngineeringDailyFeedBackBean> dataList = new ArrayList<>();

                if (feedBackMap.containsKey(date)) {
                    dataList.addAll(feedBackMap.get(date));
                } else {
                    EngineeringDailyFeedBackBean engineeringDailyFeedBackBean = new EngineeringDailyFeedBackBean();
                    engineeringDailyFeedBackBean.setNoFeedBack(true);
                    dataList.add(engineeringDailyFeedBackBean);
                }
                DailyFeedBackForEngineeringInstallAdapter adapter = new DailyFeedBackForEngineeringInstallAdapter(getActivity(), TAG);
                adapter.setData(dataList);
                adapter.setItemClick(this);
                lv_feedback.setAdapter(adapter);
//                view.setTag(dataList);

//
//                lv_feedback.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//
//
//                        EngineeringDailyFeedBackBean engineeringDailyFeedBackBean = dataList.get(0);
//                        if (engineeringDailyFeedBackBean.isNoFeedBack()) {
//
//                        } else {
//
//                            Intent mIntent = new Intent(getActivity(), DailyFeedBackDetailForEngineeringInstallActivity.class);
//                            mIntent.putExtra(ConstantUtil.KEY_1, engineeringDailyFeedBackBean);
//                            startActivity(mIntent);
//                        }
//
//
//                    }
//                });


                ll_feedback.addView(view);


            }
        }

        scrollview.smoothScrollTo(0, 0);

    }

    @Override
    public void itemClick(View view, int position) {
//        if (null != engineeringDailyFeedBackBean) {
////            if (engineeringDailyFeedBackBean.isNoFeedBack()) { //没反馈
////
////            } else {
//
//            Intent mIntent = new Intent(getActivity(), EngineeringInstallDailyFeedBackDetailActivity.class);
//            mIntent.putExtra(ConstantUtil.KEY_1, engineeringDailyFeedBackBean);
//            startActivity(mIntent);
////            }
//        }
    }
}
